/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telsoft.security.data;

import com.telsoft.security.business.SEC_BU_Group;
import com.telsoft.security.entity.SEC_ETT_Group;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author nguyenthang
 */
public class SEC_DATA_USER_GROUP {

    Connection connect = null;
    SEC_ETT_Group group;
    public SEC_ETT_Group getGroupByUserID(int USER_ID) {
        SEC_ETT_Group returnValue = new SEC_ETT_Group();
        SEC_DAOHelper connectDB = new SEC_DAOHelper();
        PreparedStatement stmt;
        ResultSet rs;
        try {
            connect = connectDB.dbConnect();
            String query = "SELECT GROUP_ID FROM SEC_USER_GROUP WHERE USER_ID ='" + USER_ID + "'";
            stmt = connect.prepareStatement(query);
            rs = stmt.executeQuery();
            while (rs.next()) {
                group = new SEC_ETT_Group();
                group = SEC_BU_Group.getGroupByID(rs.getInt("GROUP_ID"));
                returnValue.setGROUP_ID(group.getGROUP_ID());
                returnValue.setGROUP_NAME(group.getGROUP_NAME());
                returnValue.setDESCRIPTION(group.getDESCRIPTION());
                returnValue.setSTATUS(group.getSTATUS());
            }
            connectDB.dbClose(connect, stmt, rs);
        } catch (Exception ex) {
            ex.toString();
        }
        return returnValue;
    }
    
    public void addUserGroup(int UserUD, int GroupID){
        SEC_DAOHelper connectDB = new SEC_DAOHelper();
        PreparedStatement stmt;
        try {
            connect = connectDB.dbConnect();
            String query = "INSERT INTO SEC_USER_GROUP(USER_ID, GROUP_ID) VALUES(?,?)";
            stmt = connect.prepareStatement(query);
            stmt.setInt(1, UserUD);
            stmt.setInt(2, GroupID);
            stmt.execute();
            connectDB.dbClose(connect);
            connectDB.dbClose(stmt);
        } catch (Exception ex){
            ex.toString();
        }
    }
    
    public void updateUserGroup(int UserID, int GroupID){
        SEC_DAOHelper connectDB = new SEC_DAOHelper();
        PreparedStatement stmt;
        try {
            connect = connectDB.dbConnect();
            String query = "UPDATE SEC_USER_GROUP SET GROUP_ID = ? WHERE USER_ID = ?";
            stmt = connect.prepareStatement(query);
            stmt.setInt(1, GroupID);
            stmt.setInt(2, UserID);
            stmt.executeQuery();
            connectDB.dbClose(connect);
            connectDB.dbClose(stmt);
        } catch (Exception ex){
            ex.toString();
        }
    }
    
    public void deleteUserGroupByUserID(int UserID){
        SEC_DAOHelper connectDB = new SEC_DAOHelper();
        PreparedStatement stmt;
        try {
            connect = connectDB.dbConnect();
            String query = "DELETE FROM SEC_USER_GROUP WHERE USER_ID = ?";
            stmt = connect.prepareStatement(query);
            stmt.setInt(1, UserID);
            stmt.execute();
            connectDB.dbClose(connect);
            connectDB.dbClose(stmt);
        } catch (Exception ex){
            ex.toString();
        }
    }
}
